/**
 * @format
 * @Author: TC
 * @Date: 2023-03-25 21:05:00
 * @Last Modified by: TC
 * @Last Modified time: 2025-05-05 01:46:54
 */

import { __private, Component, isValid, Label, Node, Sprite, SpriteFrame } from "cc";
import { app } from "../App";
import { BaseComponent } from "../Module/Component/BaseComponent";

/**
 * ccc组件类
 */
export namespace nsComponent {
    /**
     * 判断节点上是否有指定类型的组件
     * @param node
     * @param comp
     * @returns
     */
    export function hasComponentOnNode(node: Node, comp: typeof Component): boolean {
        return node.getComponent(comp) ? true : false;
    }

    export function getComponent<T extends Component>(node: Node | T, comp: __private.__types_globals__Constructor<T>): T {
        if (!node || !isValid(node)) {
            app.log.error("ndoe is not valid");
            return null;
        }
        if (node instanceof Node) {
            return node.getComponent(comp);
        } else {
            return node;
        }
    }

    /**
     * 更新精灵或节点的纹理
     * @param node 节点
     * @param spriteFrame 纹理
     * @returns
     */
    export function updateSpriteFrame(node: Node | Sprite, spriteFrame: SpriteFrame): void {
        let sp: Sprite = null;
        if (node instanceof Node) {
            sp = node.getComponent(Sprite);
        } else {
            sp = node;
        }
        sp = getComponent(node, Sprite);
        if (!sp) {
            app.log.error("sprite is null");
            return;
        }
        sp.spriteFrame = spriteFrame;
    }

    /**
     * 设置精灵或节点的纹理
     * @param node 节点
     * @param bundleName 包名
     * @param spPath 纹理路径
     */
    export function setNodeSpriteFrame(node: Node | Sprite, bundleName: string, spPath: string): void {
        // app.resMgr.load(bundleName, spPath, ASSERT_TYPE.SPRITE_FRAME, (sp: SpriteFrame) => {
        //     updateSpriteFrame(node, sp);
        // });
    }

    /**
     * 设置节点的文本
     * @param node 节点
     * @param str 文字
     * @returns
     */
    export function setNodeLabel(node: Node | Label, str: string): void {
        let lbl: Label = null;
        if (node instanceof Node) {
            lbl = node.getComponent(Label);
        } else {
            lbl = node;
        }
        if (!lbl) {
            app.log.error("label is null");
            return;
        }
        lbl.string = str;
    }

    /**
     * 获取节点上的文本
     * @param node
     */
    export function getNodeString(node: Node | Label): string {
        let lbl: Label = null;
        if (node instanceof Node) {
            lbl = node.getComponent(Label);
        } else {
            lbl = node;
        }
        if (!lbl) {
            app.log.error("label is null");
            return "";
        }
        return lbl.string;
    }

    export function addTouchEvent(
        com: BaseComponent,
        onTouchStart?: Function,
        onTouchMove?: Function,
        onTouchEnd?: Function
    ): void {
        onTouchStart && com.node.on(Node.EventType.TOUCH_START, onTouchStart, com);
        onTouchMove && com.node.on(Node.EventType.TOUCH_MOVE, onTouchMove, com);
        onTouchEnd && com.node.on(Node.EventType.TOUCH_END, onTouchEnd, com);
        onTouchEnd && com.node.on(Node.EventType.TOUCH_CANCEL, onTouchEnd, com);
    }
}
